<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    #list_list button{line-height: 30px!important;height: 30px!important;}
    #list_list .buttonnone{margin: 5px 20px;color: #707070;background-color: #fff;border: 1px solid #e6e6e6;}
    #list_list .buttonlv{margin: 5px 20px;background-color: #248BFE}
    #list_list .chan{display: none}
    #list_list .shou{display: none}
    #list_list .zhi{display: none}
    #list_list .checkarr{display: none}
</style>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title">
        <div class="page-title-con">
            <span id="flowsetname">编辑资金成本表</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add" key="0">保存设置</button>
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add" key="1">提交</button>
                <span id="page-close" class="page-close" onclick="layer_close()"><i class="layui-icon layui-icon-close"></i></span>
            </div>
        </div>
    </div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>资金成本表</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>施工月份</th>
                            <th>本月形象进度</th>
                            <th>累计形象进度</th>
                            <th><text>*</text>产值</th>
                            <th>收款</th>
                            <th>支出</th>
                            <th>垫资利率(%)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="max-height: 300px"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<div id="jd" style="display: none;">
    <textarea id="explian" class="layui-textarea" style="height: 292px !important;"></textarea>
</div>
<script>
    var param_enginid = getQueryVariable('enginid') || 0;

    function set_chan(obj) {
        var key = $(obj).parents('tr').index();
        openPage('huizi_set_chan.php?key='+key+'&enginid='+param_enginid,'90%','90%');
    }

    function set_shou(obj) {
        var key = $(obj).parents('tr').index();
        openPage('huizi_set_shou.php?key='+key,'90%','90%');
    }

    function set_zhi(obj) {
        var key = $(obj).parents('tr').index();
        openPage('huizi_set_zhi.php?key='+key,'90%','90%');
    }

    function choose_jd(obj,type) {
        if(type == 'xingjin'){
            var title = '本月形象进度';
        }else{
            var title = '累计形象进度';
        }
        var index = $(obj).parent().parent().find('td:first-child').text();

        var text = $('#list_list tr').eq(parseInt(index) - 1).find('.'+type+'_temp').val();
        $('#explian').val(text);

        layer.open({
            type: 1,
            title: [title],
            area: ['700px', '400px'],
            btn:['确定','取消'],
            moveOut: true,
            content: $('#jd'),
            end :function () {
                var data = $('#explian').val();
                $('#list_list tr').eq(parseInt(index) - 1).find('.'+type).val(data);
                $('#list_list tr').eq(parseInt(index) - 1).find('.'+type+'_temp').val(data);
            }
        });

    }

    var qinglist;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_main_huizi_edit,data:{enginid:param_enginid}}).done(function(res){
            if (res.code === 2000) {
                qinglist = res.data.qinglist;

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input month"></td>';
                listAddStr += '<td>' +
                    '<input type="text" class="layui-input xingjin" readonly onclick="choose_jd(this,\'xingjin\')">' +
                    '<textarea style="display: none;" class="layui-textarea xingjin_temp"></textarea>' +
                    '</td>';
                listAddStr += '<td>' +
                    '<input type="text" class="layui-input xing" readonly onclick="choose_jd(this,\'xing\')">' +
                    '<textarea style="display: none;" class="layui-textarea xing_temp"></textarea>' +
                    '</td>';
                listAddStr += '<td>';
                listAddStr += '<button type="button" onclick="set_chan(this)" class="layui-btn buttonnone chanbut">';
                listAddStr += '<span>设置</span>';
                listAddStr += '<span class="chan"></span>';
                listAddStr += '</button>';
                listAddStr += '</td>';
                listAddStr += '<td>';
                listAddStr += '<button type="button" onclick="set_shou(this)" class="layui-btn buttonnone shoubut">';
                listAddStr += '<span>设置</span>';
                listAddStr += '<span class="shou"></span>';
                listAddStr += '</button>';
                listAddStr += '</td>';
                listAddStr += '<td>';
                listAddStr += '<button type="button" onclick="set_zhi(this)" class="layui-btn buttonnone zhibut">';
                listAddStr += '<span>设置</span>';
                listAddStr += '<span class="zhi"></span>';
                listAddStr += '</button>';
                listAddStr += '</td>';
                listAddStr += '<td><input type="number" class="layui-input dianrate"></td>';
                listAddStr += '<td><input class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr,1);




                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input month" value="'+list[key].month+'"></td>';
                        str += '<td>' +
                            '<input type="text" class="layui-input xingjin" readonly onclick="choose_jd(this,\'xingjin\')" value="'+list[key].xingjin+'">' +
                            '<textarea style="display: none;" class="layui-textarea xingjin_temp">'+list[key].xingjin+'</textarea></td>';
                        str += '<td>' +
                            '<input type="text" class="layui-input xing" readonly onclick="choose_jd(this,\'xing\')" value="'+list[key].xing+'">' +
                            '<textarea style="display: none;" class="layui-textarea xing_temp">'+list[key].xing+'</textarea></td>';
                        str += '<td>';
                        if(list[key].chan){
                            str += '<button type="button" onclick="set_chan(this)" class="layui-btn buttonlv chanbut">';
                        }else{
                            str += '<button type="button" onclick="set_chan(this)" class="layui-btn buttonnone chanbut">';
                        }
                        str += '<span>设置</span>';
                        str += '<span class="chan">'+list[key].chan+'</span>';
                        str += '</button>';
                        str += '</td>';
                        str += '<td>';
                        if(list[key].shou){
                            str += '<button type="button" onclick="set_shou(this)" class="layui-btn buttonlv shoubut">';
                        }else{
                            str += '<button type="button" onclick="set_shou(this)" class="layui-btn buttonnone shoubut">';
                        }
                        str += '<span>设置</span>';
                        str += '<span class="shou">'+list[key].shou+'</span>';
                        str += '<span class="checkarr">'+list[key].checkarr+'</span>';
                        str += '</button>';
                        str += '</td>';
                        str += '<td>';
                        if(list[key].zhi){
                            str += '<button type="button" onclick="set_zhi(this)" class="layui-btn buttonlv zhibut">';
                        }else{
                            str += '<button type="button" onclick="set_zhi(this)" class="layui-btn buttonnone zhibut">';
                        }
                        str += '<span>设置</span>';
                        str += '<span class="zhi">'+list[key].zhi+'</span>';
                        str += '</button>';
                        str += '</td>';
                        str += '<td><input type="number" class="layui-input dianrate" value="'+list[key].dianrate+'"></td>';
                        str += '<td><input class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                }
                $("#list_list").html(str);

                update_refresh_row_func();
                render_from();
            }
        });


        form.on('submit(add)', function(data){
            var subkey = $(this).attr('key') || 0;

            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var name = parseInt(i+1);
                    var month = $(this).find('.month').val();
                    var xing = $(this).find('.xing_temp').val();
                    var xingjin = $(this).find('.xingjin_temp').val();
                    var chan = $(this).find('.chan').text();
                    var shou = $(this).find('.shou').text();
                    var zhi = $(this).find('.zhi').text();
                    var dianrate = $(this).find('.dianrate').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!month){
                        throw new Error('第'+index+'行，请输入施工月份');
                    }
                    if(!chan){
                        throw new Error('第'+index+'行，请设置产值');
                    }


                    list.push({
                        name: name,
                        month: month,
                        xing: xing,
                        xingjin: xingjin,
                        chan: chan,
                        shou: shou,
                        zhi:zhi,
                        dianrate: dianrate,
                        explain: explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;

            data.field.enginid = param_enginid;
            facade.ajax({url: ask_main_huizi_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    if(subkey > 0){
                        dataPostTimeOut();
                    }else{
                        window.location.reload();
                    }

                }
            });
            return false;
        });

    });

</script>
</body>
</html>
